<?php

namespace IDock\Insurance\ZMBX\Response;

use IDock\Http\Response;
use IDock\IDock\IDockEnums;
use IDock\Insurance\ZMBX\ZMBXEnums;
use IDock\Printer\NonCar\NonCarInquiryPrinter;
use IDock\Printer\NonCar\NonCarInquirySinglePrinter;
use IDock\Printer\Premium\PremiumBIPrinter;
use IDock\Printer\Premium\PremiumCoveragePrinter;
use IDock\Printer\Premium\PremiumInquiryPrinter;
use IDock\Printer\SpecialClausesPrinter;

class PremiumInquiryResponse extends AllZMBXResponse
{

    /**
     * @var PremiumInquiryPrinter $printer
     * @var  $printer
     */
    public $printer = PremiumInquiryPrinter::class;

    protected $records = [];

    protected $result = [];

    public $planCode = '';

    public $productCode = '';

    public $insuredNum = 1;

    public function resolve(): Response
    {
        $body = $this->getBody();
        $body = json_decode($body, true);
        if ($this->printer) {
            $this->printer = new $this->printer;
        }
        if (empty($body)) {
            return $this;
        }
        $responseCode = $body['head']['errorCode'] ?? '0';

        $errorMsg = $body['head']['returnMessage'] ?? '';

        if (strpos($errorMsg, '重复投保') !== false) {
            $this->getRepeatData($errorMsg, '');
            if ($this->printer->repeatInsuredFlg) {
                $this->printer->success = false;
                $this->printer->repeatMessage = $errorMsg;
                return $this;
            }
        }
        if ($responseCode != '' && $responseCode != '0000') {
            $this->setError($errorMsg);
            return $this;
        }
        $this->setBody($body);
        return $this;
    }

    public function printerInit(): Response
    {
        $body = $this->getBody();

        //询价流程号
        $this->printer->argc['businessNo'] = $body['businessNo'] ?? '';
        $this->printer->inquiryNo = $body['businessNo'] ?? '';

        if ($body['bizPlatformMsg']['answer'] ?? '') {
            $this->getRepeatData('',$body['bizPlatformMsg']['answer']);
            if ($this->printer->repeatInsuredFlg) {
                $this->printer->success = false;
                $this->printer->repeatMessage = $body['bizPlatformMsg']['answer'];
                return $this;
            }
        }

        //车船税
        if ($carShipTax = $body['taxinfo'] ?? '') {
            $this->printer->carTaxPrinter->carShipTax = $carShipTax['sumPayTax'] ?? '';
            $this->printer->carTaxPrinter->overdue = $carShipTax['delayPayTax'] ?? '';
            $this->printer->carTaxPrinter->payBackTax = $carShipTax['prePayTax'] ?? '';
            $this->printer->carTaxPrinter->thisYearTax = $carShipTax['payTax'] ?? '';
        }

        //商业险
        if ($biData = $body['bizPremium'] ?? []) {
            $this->printer->biPremiumPrinter = new PremiumBIPrinter();
            $this->printer->biPremiumPrinter->biPremium = $biData['sumPremium'] ?? '';
            $this->printer->biPremiumPrinter->biDiscount = $biData['syDiscount'] ?? '';
            //浮动信息
            $adjust = $biData['adjust'] ?? [];
            foreach ($adjust as $value) {
                if ($value['adjustCode'] == 'C27') {
                    $this->printer->ncdFactor = floatval($value['adjustRate'] ?? '') / 100;
                } else {
                    //商业险系数
                    $this->printer->premiumFactorPrinter->VCIFactorAppend($value['adjustCode'], $value['adjustName'], round(floatval($value['adjustRate'] ?? '') / 100, 2));
                }
            }
        }
        $this->printer->premiumFactorPrinter->ComFactorAppend('中煤分','zmScore', round(floatval($body['platMain']['zmScore'] ?? '') / 100, 2));
        //特约
        $biEngage = $biData['engage'] ?? [];
        foreach ($biEngage as $value) {
            $specialClauses = new SpecialClausesPrinter();
            $specialClauses->clauseContent = $value['engageDetail'] ?? '';
            $specialClauses->clauseName = $value['engageTitle'] ?? '';
            $specialClauses->autoFlag = 0;
            $specialClauses->riskType = 'VCI';
            $this->printer->specialClausesAppend($specialClauses);
        }
        //交强险
        if ($ciData = $body['forcePrimium'] ?? []) {
            $this->printer->ciPremiumPrinter->ciPremium = $ciData['premium'] ?? '';
            $this->printer->ciPremiumPrinter->ciDiscount = $ciData['jqDiscount'] ?? '';
            //因子
            $floatFactor = $ciData['accidentBeanlist'] ?? [];
            foreach ($floatFactor as $value) {
                $this->printer->premiumFactorPrinter->TCIFactorAppend($value['adjustCode'], $value['adjustName'], round(floatval($value['adjustRate'] ?? '') / 100, 2));
            }
        }
        $ciEngage = $ciData['engage'] ?? [];
        foreach ($ciEngage as $value) {
            $specialClauses = new SpecialClausesPrinter();
            $specialClauses->clauseContent = $value['engageDetail'] ?? '';
            $specialClauses->clauseName = $value['engageTitle'] ?? '';
            $specialClauses->autoFlag = 0;
            $specialClauses->riskType = 'TCI';
            $this->printer->specialClausesAppend($specialClauses);
        }
        //险种
        if ($kinds = $biData['coverageList'] ?? []) {
            foreach ($kinds as $kind) {
                $coverage = new PremiumCoveragePrinter();
                $coverage->coverageCode = array_search($kind['kindCode'], ZMBXEnums::$coverageCode, true);
                $coverage->coverageName = IDockEnums::$coverageCode[$coverage->coverageCode] ?? '';
                $coverage->premium = $kind['premium'] ?? '';
                $coverage->basePremium = $kind['benchMarkPremium'] ?? '';
                $coverage->amount = $kind['amount'] ?? '';
                $coverage->quantity = $kind['quantity'] ?? '';
                if (in_array($coverage->coverageCode, ['C08001', 'C08002', 'C08003', 'C08004', 'C18001', 'C18002', 'C18003', 'C18004'])) {
                    $coverage->serviceTimes = $kind['quantity'] ?? '';
                }
                $this->printer->coverageListAppend($coverage);
            }
        }

        //非车
        if ($body['accidentPremium'] ?? []) {
            $this->printer->nonCarInquiryPrinter = new NonCarInquiryPrinter();

            foreach ($body['accidentPremium'] ?? [] as $value) {

                $single = new NonCarInquirySinglePrinter();
                $single->premium = $value['sumPremium'] ?? '';
                $single->inquiryNo = null;
                $single->amount = $value['insuranceAmount'] ?? '';
                $single->statusCode = 'success';
                $single->planCode = $value['rideRiskCode'] ?? '';

                $this->printer->nonCarInquiryPrinter->totalPremium += $single->premium;
                $this->printer->nonCarInquiryPrinter->noCarInquirySingleAppend($single);
            }
        }

        return $this;
    }

    public function newEnergy()
    {
        $list = $this->printer->coverageList;
        /***
         * @var PremiumCoveragePrinter $value
         */
        foreach ($list as $value) {
            $value->coverageCode = 'C1' . substr($value->coverageCode, 2);
            $value->coverageName = IDockEnums::$coverageCode[$value->coverageCode] ?? '';
        }
    }

    public function getRepeatData($ciMeg, $biMeg)
    {

        if (preg_match('/终保日期 ([\d\s:-]+)/', $ciMeg, $matches)) {
            if ($matches[1] ?? '') {
                $ciStartData = date('Y-m-d H:i:s', strtotime($matches[1]));
                $this->printer->repeatInsurePrinter['ciStartData'] = $ciStartData;
                $this->printer->repeatInsuredFlg = true;
            }
        }

        if (preg_match('/起保日期： ([\d\s:-]+)/', $biMeg, $matches)) {
            if ($matches[1] ?? '') {
                $biStartData = date('Y-m-d H:i:s', strtotime($matches[1]));
                $this->printer->repeatInsurePrinter['biStartData'] = $biStartData;
                $this->printer->repeatInsuredFlg = true;
            }
        }
    }
}
